package com.zhsm.fuxi.mvp.ui.dialog

import android.content.Context
import android.view.View
import com.hankkin.reading.control.UserControl
import com.zhsm.paipaiout.baselibrary.utils.ToastUtils
import com.zhsm.fuxi.R
import com.zhsm.fuxi.base.BaseDialog
import com.zhsm.paipaiout.baselibrary.widget.view.MyTextWatcher
import kotlinx.android.synthetic.main.dialog_mycenter.*
import org.jetbrains.anko.sdk25.coroutines.onClick

/**
 * Created by GuZhC on 2018/10/30.
 * describe: 以后别这样写  乱！！！！！！！
 */
class MyCenterDialog(context: Context) : BaseDialog(context) {

    var wht: Int = CHANGE_HEAD
        set(value) {
            field = value
            changeView(value)
        }

    companion object {
        val CHANGE_HEAD = 1
        val CHAGE_EMAIL = 2
        val CHAGE_IMG = 3
        val LOGIN_OUT = 4
        val CHAGE_PHONE = 5
    }

    override fun getLayoutId(): Int {
        return R.layout.dialog_mycenter
    }

    override fun getGravity(): Int {
        return BaseDialog.GRAVITY_CENTER
    }

    override fun isCanceledOnTouchOutside(): Boolean {
        return true
    }

    override fun initView() {
        super.initView()
        setClick()
        changeView(wht)
        myCenterDialogInput.addTextChangedListener(object : MyTextWatcher() {
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                if (s.isNullOrEmpty()) myCenterDialogDelete.visibility = View.GONE
                else myCenterDialogDelete.visibility = View.VISIBLE
            }
        })
        myCenterDialogDelete.onClick { myCenterDialogInput.setText("") }
        dialogRoot.onClick { dismiss() }
    }

    private fun changeView(value: Int) {
        when (value) {
            CHANGE_HEAD -> {
                isChangeImg(0)
                myCenterDialogTitle.text = context.resources.getString(R.string.set_nick)
                UserControl.getCurrentUser()?.let {
                    myCenterDialogInput.setText(it.nick)
                }
            }
            CHAGE_EMAIL -> {
                isChangeImg(0)
                myCenterDialogTitle.text = context.resources.getString(R.string.set_email)
                UserControl.getCurrentUser()?.let {
                    myCenterDialogInput.setText(it.email)
                }
            }
            CHAGE_IMG -> {
                isChangeImg(1)
                myCenterDialogTitle.text = context.resources.getString(R.string.set_img)
            }
            LOGIN_OUT -> {
                isChangeImg(2)
                myCenterDialogTitle.text = context.resources.getString(R.string.isout_login)
            }
            CHAGE_PHONE -> {
                isChangeImg(0)
                myCenterDialogTitle.text = context.resources.getString(R.string.set_phone)
                UserControl.getCurrentUser()?.let {
                    myCenterDialogInput.setText(it.mobile)
                }
            }
        }
    }

    private fun isChangeImg(w: Int) {
        when (w) {
            1 -> {
                myCenterDialogllInput.visibility = View.GONE
                myCenterDialogllpic.visibility = View.VISIBLE
                myCenterDialogCancel.visibility = View.VISIBLE
                myCenterDialogSave.visibility = View.GONE
                myCenterDialogOutHint.visibility = View.GONE
                myCenterDialogLlOut.visibility = View.GONE
            }
            0 -> {
                myCenterDialogllInput.visibility = View.VISIBLE
                myCenterDialogllpic.visibility = View.GONE
                myCenterDialogCancel.visibility = View.GONE
                myCenterDialogSave.visibility = View.VISIBLE
                myCenterDialogOutHint.visibility = View.GONE
                myCenterDialogLlOut.visibility = View.GONE
            }
            else -> {
                myCenterDialogllInput.visibility = View.GONE
                myCenterDialogllpic.visibility = View.GONE
                myCenterDialogCancel.visibility = View.GONE
                myCenterDialogSave.visibility = View.GONE
                myCenterDialogOutHint.visibility = View.VISIBLE
                myCenterDialogLlOut.visibility = View.VISIBLE

            }
        }

    }

    fun setClick() {
        myCenterDialogTitle.onClick {
            if (wht != CHAGE_IMG) return@onClick
            mClickTakePhotoAndOut?.takePic()
            dismiss()
        }
        myCenterDialogChosePic.onClick {
            mClickTakePhotoAndOut?.chosePic()
            dismiss()
        }
        myCenterDialogCancel.onClick { dismiss() }
        myCenterDialogSave.onClick {
            if (myCenterDialogInput.text.isEmpty()) {
                ToastUtils.makeText("内容不能为空")
                return@onClick
            }
            mClickSave?.saveInfo(myCenterDialogInput.text.toString().trim(), wht)
        }
        myCenterDialogNoOut.onClick { dismiss() }
        myCenterDialogOut.onClick {
            mClickTakePhotoAndOut?.loginOut()
            dismiss()
        }
    }

    override fun dismiss() {
        super.dismiss()
        mClickSave = null
        mClickTakePhotoAndOut = null
    }

    interface onClickSave {
        //保存
        fun saveInfo(s: String, wht: Int)
    }

    private var mClickSave: onClickSave? = null

    fun setOnClickSave(mClickSave: onClickSave) {
        this.mClickSave = mClickSave
    }

    interface onClickTakePhotoAndOut {
        //拍照
        fun takePic()

        //相册
        fun chosePic()
        fun loginOut()
    }

    private var mClickTakePhotoAndOut: onClickTakePhotoAndOut? = null

    fun setonClickTakePhotoAndOut(mOnClickTakePhoto: onClickTakePhotoAndOut) {
        this.mClickTakePhotoAndOut = mOnClickTakePhoto
    }
}